package com.gzdh.tang.modu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzdh.tang.modu.entity.MacMainpSbruanKlButie;
import com.gzdh.tang.modu.mapper.MacMainpSbruanKlButieMapper;
import com.gzdh.tang.modu.service.MacMainpSbruanKlButieService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service("macMainpSbruanKlButieService")
public class MacMainpSbruanKlButieServiceImpl extends ServiceImpl<MacMainpSbruanKlButieMapper,
        MacMainpSbruanKlButie>
        implements MacMainpSbruanKlButieService {


    @Override
    public Map<String, String> getPayPrice(String kl, String dinner, String payPrice) {
        Map<String, String> res = new HashMap<>();
        res.put("amount", payPrice);
        MacMainpSbruanKlButie macMainpSbruanKlButie = this.getOne(new QueryWrapper<MacMainpSbruanKlButie>()
                .lambda().eq(MacMainpSbruanKlButie::getKl, kl).eq(MacMainpSbruanKlButie::getDinner, dinner));
        if (macMainpSbruanKlButie == null) {
            return res;
        }
        BigDecimal payPriceDec = new BigDecimal(payPrice);
        switch (macMainpSbruanKlButie.getType()) {
            case 1:
                res.putAll(subtractPrice(macMainpSbruanKlButie, payPriceDec));
                break;
        }
        return res;
    }


    private Map<String, String> subtractPrice(MacMainpSbruanKlButie macMainpSbruanKlButie, BigDecimal payPrice) {

        Map<String, String> res = new HashMap<>();
        BigDecimal dPayPrice = payPrice.subtract(new BigDecimal(macMainpSbruanKlButie.getMinPrice()));
        if (dPayPrice.compareTo(new BigDecimal(0)) <= 0) {
            //待优惠金额为0
            res.put("accountPrice", macMainpSbruanKlButie.getMinPrice());
            res.put("youhuiPrice", "0");
            return res;
        }
        BigDecimal priceTemp = dPayPrice.subtract(new BigDecimal(macMainpSbruanKlButie.getButie()));
        if (priceTemp.compareTo(new BigDecimal(0)) <0) {
            //当小于或等于0时，则在优惠区间内
            res.put("accountPrice", macMainpSbruanKlButie.getMinPrice());
            res.put("youhuiPrice", priceTemp.toPlainString());
        } else {
            //减去优惠还需要自费
            res.put("youhuiPrice", macMainpSbruanKlButie.getButie());
            res.put("accountPrice", new BigDecimal(macMainpSbruanKlButie.getMinPrice()).add(priceTemp).toPlainString());
        }
        return res;
    }
}